<?php

class BasePage {
  const SCRIPT_TOKEN = '<!-- scripts -->';
  const CSS_TOKEN = '<!-- css -->';
  const BODY_TOKEN = '<!-- body -->';

  protected $post = '';
  protected $db = '';
  protected $css = '';
  protected $scripts = '';
  protected $language = 'en';
  protected $content = '';
  protected $page_title = '';
  protected $language_strings = array();

  function __construct($database, $page_title = '@@page_title@@', $lang = 'en') {
    $this->db = $database;
    $this->page_title = $page_title;
    $this->language = $lang;
  }

  /**
   * Allows for updating the current language to use.
   *  - extending classes should call this out first and then check the return value in order to change language bindings.
   *
   */
  function set_language($new_lang) {
    if(!empty($new_lang)) {
      // linux is case sensitive
      $new_lang = strtolower(trim($new_lang));
      if(file_exists("lib/l10n/$new_lang/main.php") && is_readable("lib/l10n/$new_lang/main.php")) {
        $this->language = $new_lang;
        $this->language_file = "lib/l10n/{$this->language}/main.php";
        // fills $tmp_language_strings
        include_once $this->language_file;
        $this->_set_language_strings($tmp_language_strings);
        return true;
      }
    }
    error_log('BasePage.set_language(): Could not set new language - '.$new_lang);
    return false;
  }

  function get_language() {
    return $this->language;
  }

  private function _set_language_strings($new_array) {
    $this->language_strings = $new_array;
  }

  protected function add_language_strings($new) {
    $this->language_strings = array_merge($this->language_strings, $new);
  }

  function add_post($post) {
    $this->post = $post;
  }

  /**
   * The function that is called to produce the entire page
   *
   * @var string $content is the body content of this page
   */
  function tinkle($content = '') {
    if(!empty($content)) {
      $this->content = $content;
    } else {
      error_log("main->tinkle(): content empty");
    }
    // returns FALSE if the file is not found
    if($template = file_get_contents('templates/main.inc')) {
      // spit it out, 
      echo $template;
    }
  }

  function output_callback($output) {
    //error_log("main->output_callback(): $output");
    if(strpos($output, $this::SCRIPT_TOKEN) > 0 && !empty($this->scripts)) {
      $output = str_replace($this::SCRIPT_TOKEN, $this->scripts, $output);
    }
    if(strpos($output, $this::CSS_TOKEN) > 0 && !empty($this->css)) {
      $output = str_replace($this::CSS_TOKEN, $this->css, $output);
    }
    // add the page content if there is one
    if(!empty($this->content)) {
      if(strpos($output, $this::BODY_TOKEN) > 0) {
        $output = str_replace($this::BODY_TOKEN, $this->content, $output);
      } else {
        error_log("main->output_callback(): could not find ".$this::BODY_TOKEN);
      }
    }
    //error_log("language_strings: ".var_export($this->language_strings,true));
    // cycle through each localized token
    foreach($this->language_strings as $token => $string) {
      if(strpos($output, $token) > 0) {
        $output = str_replace($token, $string, $output);
      }
    }
    // these are special and need to be overriden at this time in order to make sure the page displays properly
    if(strpos($output, '@@page_title@@') > 0) {
      $output = str_replace('@@page_title@@', $this->page_title, $output);
    }
    if(strpos($output, '@@lang@@') > 0) {
      $output = str_replace('@@lang@@', $this->language, $output);
    }
    //error_log("main->output_callback(): $output");
    return $output;
  }
}
?>
